<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="帖子唯一id" prop="postId">
        <el-input
            v-model="queryParams.postId"
            placeholder="请输入帖子唯一id"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布账号" prop="userAccount">
        <el-input
            v-model="queryParams.userAccount"
            placeholder="请输入发布账号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
            v-model="queryParams.nickName"
            placeholder="请输入用户昵称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帖子类别" prop="postType">
        <el-select v-model="queryParams.postType" placeholder="请选择帖子类别" clearable>
          <el-option
              v-for="dict in post_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="帖子标题" prop="postTitle">
        <el-input
            v-model="queryParams.postTitle"
            placeholder="请输入帖子标题"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布日期" prop="publishDate">
        <el-date-picker clearable
                        v-model="queryParams.publishDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择发布日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="帖子热度" prop="hot">
        <el-input
            v-model="queryParams.hot"
            placeholder="请输入帖子热度"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['PostBarManger:PostBarManger:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['PostBarManger:PostBarManger:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['PostBarManger:PostBarManger:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['PostBarManger:PostBarManger:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="PostBarMangerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="帖子唯一id" align="center" prop="postId" />
      <el-table-column label="发布账号" align="center" prop="userAccount" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="帖子类别" align="center" prop="postType">
        <template #default="scope">
          <dict-tag :options="post_type" :value="scope.row.postType"/>
        </template>
      </el-table-column>
      <el-table-column label="帖子标题" align="center" prop="postTitle" />
      <el-table-column label="发布内容" align="center" prop="content" />
      <el-table-column label="图片地址" align="center" prop="photo" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="发布日期" align="center" prop="publishDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.publishDate, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="帖子热度" align="center" prop="hot" />
      <el-table-column label="删除状态" align="center" prop="delFlag">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.delFlag ? scope.row.delFlag.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['PostBarManger:PostBarManger:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['PostBarManger:PostBarManger:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改学校动态管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="PostBarMangerRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发布账号" prop="userAccount">
          <el-input v-model="form.userAccount" placeholder="请输入发布账号" disabled />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" disabled />
        </el-form-item>
        <el-form-item label="帖子类别" prop="postType">
          <el-select v-model="form.postType" placeholder="请选择帖子类别">
            <el-option
                v-for="dict in post_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帖子标题" prop="postTitle">
          <el-input v-model="form.postTitle" placeholder="请输入帖子标题" />
        </el-form-item>
        <el-form-item label="发布内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="图片地址" prop="photo">
          <image-upload v-model="form.photo"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PostBarManger">
import { listPostBarManger, getPostBarManger, delPostBarManger, addPostBarManger, updatePostBarManger } from "@/api/PostBarManger/PostBarManger";
//引入用户信息
import { getUserProfile } from "@/api/system/user";
const { proxy } = getCurrentInstance();
const { post_type, sys_yes_no } = proxy.useDict('post_type', 'sys_yes_no');

const PostBarMangerList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postId: null,
    userAccount: null,
    nickName: null,
    postType: null,
    postTitle: null,
    content: null,
    photo: null,
    publishDate: null,
    hot: null,
    delFlag: null
  },
  rules: {
    userAccount: [
      { required: true, message: "发布账号不能为空", trigger: "blur" }
    ],
    nickName: [
      { required: true, message: "用户昵称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询学校动态管理列表 */
function getList() {
  loading.value = true;
  listPostBarManger(queryParams.value).then(response => {
    console.log(response);
    PostBarMangerList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}



// 表单重置
function reset() {

  getUserProfile().then(response => {
    form.value.userAccount = response.data.phonenumber;
    form.value.nickName = response.data.nickName;
  })
  form.value = {
    postId: null,
    postType: null,
    postTitle: null,
    content: null,
    photo: null,
    publishDate: null,
    hot: null,
    delFlag: []
  };
  proxy.resetForm("PostBarMangerRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学校动态管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _postId = row.postId || ids.value
  getPostBarManger(_postId).then(response => {
     form.value.postId =response.data.postId
    form.value.postType = response.data.postType,
        form.value.postTitle = response.data.postTitle,
        form.value.content = response.data.content,
        form.value.photo = response.data.photo,

    open.value = true;
    title.value = "修改学校动态管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["PostBarMangerRef"].validate(valid => {
    if (valid) {
      form.value.delFlag = form.value.delFlag.join(",");
      if (form.value.postId != null) {
        updatePostBarManger(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPostBarManger(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _postIds = row.postId || ids.value;
  proxy.$modal.confirm('是否确认删除学校动态管理编号为"' + _postIds + '"的数据项？').then(function() {
    return delPostBarManger(_postIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('PostBarManger/PostBarManger/export', {
    ...queryParams.value
  }, `PostBarManger_${new Date().getTime()}.xlsx`)
}

getList();
</script>
